﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Client.Data.XmlQuery;
using Aulac.Rsc.Bss;
using Aulac.Rsc.Bss.Tags;
using Aulac.Verb;
using System.Xml;
using Aulac.Global.Xml;
using System.Data;
using Aulac.Client.Global.Interface;
using Aulac.Global.Common;
using Aulac.Global.Enums;

namespace Aulac.Client.Bss.WinForm.CapAss
{
    public partial class FormDlgStaff
    {
        /// <summary>
        /// GET thông tin chi tiết của Nhân viên.
        /// </summary>
        private bool GetDetailItemStaff()
        {
            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Aulac.Verb.AulacVerb.DataQuery.QueryData, Aulac.Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string sqlQuery = string.Format("SELECT {0}", "*");
            sqlQuery += string.Format(" FROM {0}", TableNames.Dm_Staff);             
            sqlQuery += string.Format(" WHERE {0} = @{0} ", tagItems.Staff.StaffPrkID);
            query.ObjQueryVchHeader.SqlQueryString = sqlQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagItems.Staff.StaffPrkID, tagItems.Staff.StaffPrkID, StaffPrkID);

            try
            {
                XmlDataDocument xmlDoc = query.ExecureQueryData();
                XmlElement eHeader = XmlProcess.GetVchHeaderElm(xmlDoc);
                List<IAulacDataControl> mListControls = null;
                GetListAulacDataControls(ref mListControls, grBoxInfo);
                GetListAulacDataControls(ref mListControls, groupBox1);
                foreach (IAulacValueDataCtrl ctr in mListControls)
                {
                    ctr.SetValueToControl(eHeader);
                }
               //cboCapAssType.CapAssTypePrkID = XmlProcess.GetValueVchHeader(xmlDoc, tagItems.CapAss.CapAssTypePrkID);
                comboCountry1.CountryPrkID = XmlProcess.GetValueVchHeader(xmlDoc, tagItems.Staff.CountryPrkID);
                comboDept1.DeptPrkID = XmlProcess.GetValueVchHeader(xmlDoc, tagItems.Staff.DeptPrkID);
                comboSexStaff1.SexID = XmlProcess.GetValueVchHeader(xmlDoc, tagItems.Staff.SexID);
                comboStaffType1.StaffTypePrkID = XmlProcess.GetValueVchHeader(xmlDoc, tagItems.Staff.StaffTypePrkID);
                //Dm_TbName dt = (Dm_TbName)(ValueHelper.ChangeType(XmlProcess.GetValueVchHeader(xmlDoc, tagItems.CapAss.CapAssID), typeof(Dm_TbName)));
                //cboDeptTypeID.DeptTypeID = dt;
                //comboDept1.DeptID = dt;
                //string mFncStylePrkID = XmlProcess.GetValueVchHeader(xmlDoc, tagItems.Dept.DeptFncStylePrkID);
                //if (!ValueHelper.isEmpty(mFncStylePrkID))
                //{
                //    chkFncStyle.Checked = true;
                //    cbFncStyle.FncStylePrkID = mFncStylePrkID;
                //}
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Save Item Staff.
        /// </summary>
        private bool SaveItemStaff(ref XmlDataDocument xmlData)
        {
            ClientExecXmlQueryDataCatalog query = new ClientExecXmlQueryDataCatalog();
            query.SetRequestInfo(AulacVerb.DataQuery.QueryData, AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.ObjQueryVchLines.QueryCmdType = (FormMode == FormModes.NewMode) ? QueryCmdTypes.AddDataTable : QueryCmdTypes.UpdateDataTable;

            query.ObjQueryVchLines.SetTableName(TableNames.Dm_Staff);            
            
            query.ObjQueryVchLines.AddFieldCheckValidItem(tagItems.Staff.StaffID, txtStaffID.Text);
            query.ObjQueryVchLines.AddFieldCheckValidItem(tagItems.Staff.StaffName, txtStaffName.Text);
            query.ObjQueryVchLines.AddFieldPrk(tagItems.Staff.StaffPrkID, DbType.Decimal, KeyTypes.AutoNumber);
            if (FormMode == FormModes.EditMode)
                query.ObjQueryVchLines.AddValueParamFilterItem(tagItems.Staff.StaffPrkID, tagItems.Staff.StaffPrkID, StaffPrkID);
            List<IAulacDataControl> mListControl = null;
            GetListAulacDataControls(ref mListControl, groupBox1);
            GetListAulacDataControls(ref mListControl, grBoxInfo);
            XmlElement eLine = query.ObjQueryVchLines.AddVchLineItemElm();
            foreach (IAulacValueDataCtrl ctr in mListControl)
                ctr.GetValueToXml(ref eLine);

            try
            {
                XmlDataDocument xmlDoc = query.ExecureQueryData();
                xmlData = xmlDoc;
                StaffPrkID = XmlProcess.GetValueVchLineItemFirst(xmlDoc, tagItems.Staff.StaffPrkID);
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Delete mot Item Staff.
        /// </summary>
        public bool DeleteStaff()
        {
            ClientExecXmlQueryData query = new ClientExecXmlQueryData();
            query.SetRequestInfo(Verb.AulacVerb.DataQuery.QueryData, Verb.AulacVerb.DataQuery.Noun.AulacDataQuery);
            query.SetVchHeaderElm();
            string strQuery = string.Format("DELETE FROM {0} ", TableNames.Dm_Staff);
            strQuery += string.Format(" WHERE {0} = @{0} ", tagItems.Staff.StaffPrkID);
            query.ObjQueryVchHeader.SqlQueryString = strQuery;
            query.ObjQueryVchHeader.AddValueParamFilterItem(tagItems.Staff.StaffPrkID, tagItems.Staff.StaffPrkID, StaffPrkID);

            try
            {
                query.ExecureNoneQuery();
                return true;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
    }
}
